# coding:utf-8
import os
import sys
import datetime
import set_env_path  # 请勿删除

from sqlalchemy import and_

from common.models import model_factory
from common.utils import DB


def update_follow_msg_type():
    """
    create by ld_wu 2018-06-11
    更新跟进消息类型
    """

    im_messages = model_factory.im_messages

    with DB() as session:
        messages = session.query(
            im_messages
        ).filter(
            im_messages.message_type == 40,
            and_(
                im_messages.im_to != '4',
                im_messages.im_from != '4'
            )
        ).all()

        for each in messages:
            print(each.im_from, each.im_to, each.message_type, each.group_id)
            each.message_type = 20
            each.group_id = '%s:%s:%s' % tuple(
                sorted([str(each.im_from), str(each.im_to), str(each.message_type)[0]])
            )
            print('after: ', each.im_from, each.im_to, each.message_type, each.group_id)


if __name__ == '__main__':
    update_follow_msg_type()
